package cn.muqingfeng.bbs.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import cn.muqingfeng.api.UriHelper;
import cn.muqingfeng.api.to.UserTO;
import cn.muqingfeng.bbs.R;
import cn.muqingfeng.bbs.constants.Constants;
import cn.muqingfeng.bbs.fragment.Article2ListFragment;
import cn.muqingfeng.bbs.fragment.Article3ListFragment;
import cn.muqingfeng.util.AccountManager;
import cn.muqingfeng.util.ArticleCache;
import cn.muqingfeng.util.BitmapBinder;

import com.android.volley.toolbox.NetworkImageView;

public class UcenterActivity extends Activity implements OnClickListener {
	private static final int REQUEST_CODE_TO_MY_ARTICLES = 500;
	private static final int REQUEST_CODE_TO_MY_FAV = 501;
	private TextView mUsernameView;
	private TextView mEmailView;
	private NetworkImageView mAvatarView;
	private UserTO mUserTO;
	private AlertDialog mNoLoginDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ucenter);
		setupViews();
	}

	private void setupViews() {
		findViewById(R.id.back).setOnClickListener(this);
		findViewById(R.id.about_layout).setOnClickListener(this);
		findViewById(R.id.user_info_layout).setOnClickListener(this);
		findViewById(R.id.my_post_layout).setOnClickListener(this);
		findViewById(R.id.my_fav_layout).setOnClickListener(this);
		findViewById(R.id.mark_layout).setOnClickListener(this);
		findViewById(R.id.clear_cache_layout).setOnClickListener(this);
		mUsernameView = (TextView) findViewById(R.id.username);
		mEmailView = (TextView) findViewById(R.id.email);
		mAvatarView = (NetworkImageView) findViewById(R.id.avatar);
	}

	@Override
	protected void onResume() {
		super.onResume();
		mUserTO = AccountManager.getUser(getApplicationContext());
		String email = null;
		if (mUserTO != null) {
			mUsernameView.setText(mUserTO.getUsername());
			email = mUserTO.getEmail();
		} else {
			mUsernameView.setText("您还未登录");
		}
		if (!TextUtils.isEmpty(email)) {
			mEmailView.setVisibility(View.VISIBLE);
			mEmailView.setText(email);
		} else {
			mEmailView.setVisibility(View.GONE);
		}
		if (mUserTO != null) {
			String avatarUrl = UriHelper.getAvatarUrl(mUserTO.getUid());
			BitmapBinder.bindNetImage(getApplicationContext(), mAvatarView, avatarUrl, R.drawable.ic_no_login);
		} else {
			mAvatarView.setDefaultImageResId(R.drawable.ic_no_login);
			mAvatarView.setErrorImageResId(R.drawable.ic_no_login);
			mAvatarView.setImageResource(R.drawable.ic_no_login);
		}
	}

	private synchronized void showNOLoginDialog(final int requestCode) {
		if (mNoLoginDialog != null && mNoLoginDialog.isShowing()) {
			return;
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("提示");
		builder.setMessage("您还未登录，是否立即登录？");
		builder.setNegativeButton("否", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.setPositiveButton("是", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				toLoginActivity(requestCode);
			}
		});
		mNoLoginDialog = builder.create();
		mNoLoginDialog.show();
	}

	private void toLoginActivity(int requestCode) {
		Intent intent = new Intent(this, LoginActivity.class);
		intent.putExtra(Constants.EXTRA_ACTION, LoginActivity.ACTION_RETURN);
		startActivityForResult(intent, requestCode);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK)
			return;
		UserTO userTO = AccountManager.getUser(getApplicationContext());
		if (userTO == null)
			return;
		switch (requestCode) {
		case REQUEST_CODE_TO_MY_ARTICLES: {
			Intent intent = new Intent(this, SimpleListActivity.class);
			intent.putExtra(Constants.EXTRA_FRAGMENT, Article2ListFragment.class.getName());
			Bundle bundle = new Bundle();
			bundle.putString(Constants.EXTRA_URL, UriHelper.getMyArticleUrl());
			intent.putExtra(Constants.EXTRA_BUNDLE, bundle);
			intent.putExtra(Constants.EXTRA_TITLE, "我的帖子");
			startActivity(intent);
		}
			break;
		case REQUEST_CODE_TO_MY_FAV: {
			Intent intent = new Intent(this, SimpleListActivity.class);
			intent.putExtra(Constants.EXTRA_FRAGMENT, Article3ListFragment.class.getName());
			Bundle bundle = new Bundle();
			bundle.putString(Constants.EXTRA_URL, UriHelper.getMyFavArticleUrl());
			intent.putExtra(Constants.EXTRA_BUNDLE, bundle);
			intent.putExtra(Constants.EXTRA_TITLE, "我的收藏");
		}
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.about_layout: {
			Intent intent = new Intent(this, AboutActivity.class);
			startActivity(intent);
		}
			break;
		case R.id.user_info_layout: {
			if (mUserTO == null) {
				Intent intent = new Intent(this, LoginActivity.class);
				startActivity(intent);
			} else {
				Intent intent = new Intent(this, AccountActivity.class);
				startActivity(intent);
			}
		}
			break;
		case R.id.my_post_layout: {
			if (mUserTO != null) {
				Intent intent = new Intent(this, SimpleListActivity.class);
				intent.putExtra(Constants.EXTRA_FRAGMENT, Article2ListFragment.class.getName());
				Bundle bundle = new Bundle();
				bundle.putString(Constants.EXTRA_URL, UriHelper.getMyArticleUrl());
				intent.putExtra(Constants.EXTRA_BUNDLE, bundle);
				intent.putExtra(Constants.EXTRA_TITLE, "我的帖子");
				startActivity(intent);
			} else {
				showNOLoginDialog(REQUEST_CODE_TO_MY_ARTICLES);
			}
		}
			break;
		case R.id.my_fav_layout: {
			if (mUserTO != null) {
				Intent intent = new Intent(this, SimpleListActivity.class);
				intent.putExtra(Constants.EXTRA_FRAGMENT, Article3ListFragment.class.getName());
				Bundle bundle = new Bundle();
				bundle.putString(Constants.EXTRA_URL, UriHelper.getMyFavArticleUrl());
				intent.putExtra(Constants.EXTRA_BUNDLE, bundle);
				intent.putExtra(Constants.EXTRA_TITLE, "我的收藏");
				startActivity(intent);
			} else {
				showNOLoginDialog(REQUEST_CODE_TO_MY_FAV);
			}
		}
			break;
		case R.id.mark_layout: {
			Uri uri = Uri.parse("market://details?id=" + getPackageName());
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
		}
			break;
		case R.id.clear_cache_layout:
			ArticleCache.clearCache(getApplicationContext());
			BitmapBinder.clearCache(getApplicationContext());
			Toast.makeText(getApplicationContext(), "已清除缓存", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
	}
}
